//
//  HTDataBase_DB.m
//  豆瓣
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "HTDataBase_DB.h"

@implementation HTDataBase_DB

+ (HTDataBase_DB *)shareData{
    static HTDataBase_DB *dataBase_DB = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataBase_DB = [[HTDataBase_DB alloc] init];
    });
    return dataBase_DB;
}



- (void)opDataBase{
  
    if (db_DB == nil) {
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        NSString *dbPath = [documentPath stringByAppendingPathComponent:@"DB.sqlite"];
        NSLog(@"DBAdress:%@",dbPath);
        int result = sqlite3_open(dbPath.UTF8String, &db_DB);
        if (result == SQLITE_OK) {
//            NSLog(@"打开数据库成功");
        }else{
//            NSLog(@"打开数据库失败");
        }
    }
  
}

- (void)closeDataBase{
    
    if (db_DB != nil) {
        int result = sqlite3_close(db_DB);
        if (result == SQLITE_OK) {
//            NSLog(@"数据库关闭成功");
            db_DB = nil;
        }else{
//            NSLog(@"数据库关闭失败");
        }
    }
}

- (void)createDataBase{
    
    //建表Activity
    NSString *dbStr_Activity = [NSString stringWithFormat:@"create table if not exists Activity(number integer primary  key autoincrement, title text)"];
    int result_Activity = sqlite3_exec(db_DB, dbStr_Activity.UTF8String, NULL, NULL, NULL);
    if (result_Activity == SQLITE_OK) {
//        NSLog(@"建表成功_Activity");
    }else{
//        NSLog(@"建表失败_Activity");
    }
    
    //建表Movie
    NSString *dbStr_Movie = [NSString stringWithFormat:@"create table if not exists Movie(number integer primary  key autoincrement, title text)"];
    int result_Movie = sqlite3_exec(db_DB, dbStr_Movie.UTF8String, NULL, NULL, NULL);
    if (result_Movie == SQLITE_OK) {
//        NSLog(@"建表成功_Movie");
    }else{
//        NSLog(@"建表失败_Movie");
    }
    
    //建表State
    NSString *dbStr_State = [NSString stringWithFormat:@"create table if not exists State(number integer primary  key autoincrement, title text)"];
    int result_State = sqlite3_exec(db_DB, dbStr_State.UTF8String, NULL, NULL, NULL);
    if (result_State == SQLITE_OK) {
//        NSLog(@"建表成功_State");
    }else{
//        NSLog(@"建表失败_State");
    }
    
}

- (void)insertModel:(Model_DB *)model Table:(NSString *)tableName{

    NSString *dbStr = [NSString stringWithFormat:@"insert into %@(title) values('%@')", tableName, model.title];
    int result = sqlite3_exec(db_DB, dbStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
//        NSLog(@"插入成功");
    }else{
//        NSLog(@"插入失败");
    }
}

- (void)deleteModel:(Model_DB *)model Table:(NSString *)tableName{
    
    NSString *dbStr = [NSString stringWithFormat:@"delete from %@ where title = '%@'", tableName, model.title];
    int result = sqlite3_exec(db_DB, dbStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
//        NSLog(@"删除成功");
    }else{
//        NSLog(@"删除失败");
    }
    
}

- (NSMutableArray *)selectModel:(Model_DB *)model Table:(NSString *)tableName{
    
    NSString *sqlStr = [NSString stringWithFormat:@"select *from %@ where title = '%@'", tableName, model.title];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare(db_DB, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            Model_DB *model = [[Model_DB alloc] init];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            [array addObject:model];
        }
    }
    return array;
}

- (NSMutableArray *)selectAllFromTable:(NSString *)tableName{
    
    NSString *sqlStr = [NSString stringWithFormat:@"select * from '%@'", tableName];
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db_DB, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            Model_DB *model = [[Model_DB alloc] init];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            [array addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    return array;
}

@end
